package com.laomuji.android.mvvm.view

import android.content.Context
import android.graphics.BlurMaskFilter
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.widget.LinearLayout
import androidx.core.content.withStyledAttributes
import com.laomuji.android.mvvm.R
import com.laomuji.android.mvvm.utils.ResourceUtil

/**
 * 自定义CardView
 * 原生的CardView会在某些低版本手机上显示异常
 * @author laomuji666
 * @since 2024/2/21
 */
class CustomCardView: LinearLayout {
    //阴影最大偏移量
    private val shadowOffset: Float = ResourceUtil.dpToPx(20)

    //阴影最大模糊半径
    private val shadowBlurMax: Float = ResourceUtil.dpToPx(20)

    //默认模糊半径
    private val shadowBlur: Float = ResourceUtil.dpToPx(5)

    //模糊度半径
    private var blurRadius: Float = shadowBlur

    //水平位移
    private var xOffset = ResourceUtil.dpToPx(10)

    //竖直方向位移
    private var yOffset = ResourceUtil.dpToPx(10)

    private val mPaint = Paint()
    private val locationPaint = Paint()

    //阴影颜色
    private var shadowColor = 0

    //背景色
    private var bgColor = Color.WHITE

    //阴影半径
    private var shadowRadius = 0f

    //位置
    private var left = 0
    private var right = 0
    private var top = 0
    private var bottom = 0

    constructor(context: Context):this(context,null)

    constructor(context: Context,attrs: AttributeSet?):super(context,attrs){
        setLayerType(LAYER_TYPE_SOFTWARE, null) //取消硬件加速

        context.withStyledAttributes(attrs, R.styleable.CustomCardView) {
            shadowColor = getColor(R.styleable.CustomCardView_crvShadowColor, Color.BLUE)
            blurRadius = getDimension(
                R.styleable.CustomCardView_crvBlurRadius,
                shadowBlur
            )
            shadowRadius = getDimension(R.styleable.CustomCardView_crvShadowRadius, 0f)
            xOffset = getDimension(R.styleable.CustomCardView_crvXOffset, 0f)
            yOffset = getDimension(R.styleable.CustomCardView_crvYOffset, 0f)
            bgColor = getColor(R.styleable.CustomCardView_crvBgColor, Color.WHITE)
        }

        if (shadowRadius < 0) {
            shadowRadius = -shadowRadius
        }
        if (blurRadius < 0) {
            blurRadius = -blurRadius
        }

        blurRadius = Math.min(shadowBlurMax, blurRadius)

        if (Math.abs(xOffset) > shadowOffset) {
            xOffset =
                xOffset / Math.abs(xOffset) * shadowOffset
        }

        if (Math.abs(yOffset) > shadowOffset) {
            yOffset =
                yOffset / Math.abs(yOffset) * shadowOffset
        }

        init()
    }

    private fun init() {
        setBackgroundColor(ResourceUtil.getColor(R.color.color_FFFFFF_0))
        if (xOffset > 0) {
            //水平偏移量为正数，右侧有阴影，阴影长度为blurRadius+|xOffset|
            right = (blurRadius + Math.abs(xOffset)).toInt()
        } else if (xOffset == 0f) {
            //水平偏移为0,水平间距为blurRadius
            left = blurRadius.toInt()
            right = blurRadius.toInt()
        } else {
            //水平偏移为负数,左侧有阴影，阴影长度为blurRadius+|xOffset|
            left = (blurRadius + Math.abs(xOffset)).toInt()
        }
        if (yOffset > 0) {
            //竖直偏移量为正数，底部有阴影，阴影长度为blurRadius+|yOffset|
            bottom = (blurRadius + Math.abs(yOffset)).toInt()
        } else if (yOffset == 0f) {
            //竖直偏移量为0，竖直间距为blurRadius
            top = blurRadius.toInt()
            bottom = blurRadius.toInt()
        } else {
            //竖直偏移量为负数，顶部有阴影，阴影长度为blurRadius+|yOffset|
            top = (blurRadius + Math.abs(yOffset)).toInt()
        }
        setPadding(left, top, right, bottom)
    }
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        drawBackground(canvas)
    }

    //绘制背景色(在子view底部)
    private fun drawBackground(canvas: Canvas) {
        val mWidthMode = measuredWidth.toFloat()
        val mHeightMode = measuredHeight.toFloat()
        val startX: Float
        val startY: Float
        val endX: Float
        val endY: Float
        if (xOffset == 0f) {
            startX = right.toFloat()
            endX = mWidthMode - blurRadius
        } else {
            startX = right + blurRadius
            endX = mWidthMode - left - blurRadius
        }
        if (yOffset == 0f) {
            startY = bottom.toFloat()
            endY = mHeightMode - blurRadius
        } else {
            startY = bottom + blurRadius
            endY = mHeightMode - top - blurRadius
        }
        if (blurRadius > 0) {
            mPaint.setMaskFilter(BlurMaskFilter(blurRadius, BlurMaskFilter.Blur.NORMAL))
        }
        mPaint.color = shadowColor
        mPaint.isAntiAlias = true
        val shadowRect = RectF(startX, startY, endX, endY)
        val locationRectF =
            RectF(left.toFloat(), top.toFloat(), mWidthMode - right, mHeightMode - bottom)
        if (shadowRadius == 0f) {
            //不是圆角
            canvas.drawRect(shadowRect, mPaint)
        } else {
            //圆角，角度为shadowRadius
            canvas.drawRoundRect(shadowRect, shadowRadius, shadowRadius, mPaint)
        }
        locationPaint.color = bgColor
        locationPaint.isAntiAlias = true
        if (shadowRadius == 0f) {
            //不是圆角
            canvas.drawRect(locationRectF, locationPaint)
        } else {
            //圆角，角度为shadowRadius
            canvas.drawRoundRect(locationRectF, shadowRadius, shadowRadius, locationPaint)
        }
    }
}